/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package poli.reserva.bean;

import java.io.Serializable;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import poli.reserva.dao.JLoginMySQL;

/**
 *
 * @author DAVID MOTTA
 */

@ManagedBean
@SessionScoped
public class CrearUsuario extends Object implements Serializable{
    
    private String cedula;
    
    private String nombre;
    
    private String apellido;
   
    private String email;
    
    private String pass;
    
    private String idusuario;
    
    private boolean loggedIn;
    
    private boolean createUser;
    
    @ManagedProperty(value="#{navigationBean}")
	private NavigationBean navigationBean;
    
    
    public CrearUsuario(){
        
    }

    public CrearUsuario(String cedula, String nombre, String apellido, String email, String pass, String idusuario) {
        this.cedula = cedula;
        this.nombre = nombre;
        this.apellido = apellido;
        this.email = email;
        this.pass = pass;
        this.idusuario = idusuario;
    }

    public String getCedula() {
        return cedula;
    }

    public String getNombre() {
        return nombre;
    }

    public String getApellido() {
        return apellido;
    }

    public String getEmail() {
        return email;
    }

    public String getPass() {
        return pass;
    }

    public String getIdusuario() {
        return idusuario;
    }

    public boolean isLoggedIn() {
        return loggedIn;
    }

    public boolean isCreateUser() {
        return createUser;
    }

    public NavigationBean getNavigationBean() {
        return navigationBean;
    }

    public void setCedula(String cedula) {
        this.cedula = cedula;
    }

    public void setNombre(String nombre) {
        this.nombre = nombre;
    }

    public void setApellido(String apellido) {
        this.apellido = apellido;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public void setPass(String pass) {
        this.pass = pass;
    }

    public void setIdusuario(String idusuario) {
        this.idusuario = idusuario;
    }

    public void setLoggedIn(boolean loggedIn) {
        this.loggedIn = loggedIn;
    }

    public void setCreateUser(boolean createUser) {
        this.createUser = createUser;
    }

    public void setNavigationBean(NavigationBean navigationBean) {
        this.navigationBean = navigationBean;
    }

    @Override
    public String toString() {
        return "crearUsuario{" + "cedula=" + cedula + ", nombre=" + nombre + ", apellido=" + apellido + ", email=" + email + ", pass=" + pass + ", idusuario=" + idusuario + ", loggedIn=" + loggedIn + ", createUser=" + createUser + ", navigationBean=" + navigationBean + '}';
    }
    
    public String ResgistrarUsuario(){
        
        JLoginMySQL users = new JLoginMySQL();
        
        Login crear  = new Login(cedula, nombre, apellido, email, pass, idusuario);
        
       boolean insercion =  users.insertJusuarioMySQL(crear);
       
       if (insercion== true){
            createUser = true;
           
       }
        
        FacesMessage msg = new FacesMessage("Creacion fallida!", "ERROR MSG");
                msg.setSeverity(FacesMessage.SEVERITY_ERROR);
                FacesContext.getCurrentInstance().addMessage(null, msg);
		
		// To to login page
		return navigationBean.toLogin();
        
        
        
    } 
    
     public static boolean validarEmail(String email) {
   	 String regex = "^\\w+([\\.-]?\\w+)*@\\w+([\\.-]?\\w+)*(\\.\\w{2,3})+$";
   	 Pattern pattern = Pattern.compile(regex);
   	 Matcher matcher = pattern.matcher(email);
   	 return matcher.matches();
   	 }
    
}
